package com.cnbot.irobotvoice.api;

import android.util.Log;

import com.cnbot.irobotvoice.listener.IRobotNlpResultListener;
import com.xiaoi.why.sdk.utils.callback.NLPCallback;
import com.xiaoi.why.sdk.vcloud.nlp.NLPManager;

/**
 * Copyright (c) 2016--2019/2/21  Hunan Cnbot Co., Ltd. All Rights Reserved.
 *
 * @descriptoin 小i 语义分析
 * @FileName: IRobotNlpTools.java
 * @author: dc
 * @date: 2019/2/21 11:12
 * @version: 1.0
 */

public class IRobotNlpTools {

    public static final String TAG = IRobotNlpTools.class.getSimpleName();

    /**
     * 小i 语义引擎
     */
    private NLPManager nlpManager = null;


    /**
     * 语义分析结果监听器
     */
    private IRobotNlpResultListener iRobotNlpResultListener;

    private static IRobotNlpTools instance;



    /**
     * 正式运行服务器地址
     * http://10.255.30.91:8000/robot/ask.action
     */
    String RELEASE_BASE_NLP_URL = "10.255.30.91:8000";
    /**
     * http://bjxjc.demo.xiaoi.com/robot/ask.action
     * debug运行服务器地址
     */
    String DEBUG_BASE_NLP_URL = "bjxjc.demo.xiaoi.com";

    /**
     * 服务器地址, 根据buildConfig的变量DEBUG_SERVER_URL 获取不同的服务器地址
     */
    String NLP_URL ;

    public IRobotNlpTools(boolean isDebug) {
        /**
         * 服务器地址, 根据buildConfig的变量DEBUG_SERVER_URL 获取不同的服务器地址
         */
        NLP_URL = isDebug == false ? "http://" + RELEASE_BASE_NLP_URL + "/robot/ask.action" : "http://" + DEBUG_BASE_NLP_URL + "/robot/ask.action";

    }


    public static IRobotNlpTools getInstance(boolean isDebug){
        if(instance == null){
            synchronized (IRobotNlpTools.class){
                if(instance == null){
                    instance = new IRobotNlpTools(isDebug);
                }
            }
        }
        return instance;
    }

    /**
     * 初始化NLP
     *
     */
    public void initNlp(){
        nlpManager = new NLPManager.Builder()
                .appKey("9516d09e23e34ae38687fc6b2e038d7c")
                .appSecret("0fd08ffd50944af5811cc1412d682a2e")
                .url(NLP_URL)
                .signature("ask")
                .build();
        Log.e(TAG, "NLP_URL = " + NLP_URL);

    }


    /**
     * 注册语义分析结果监听器
     * @param iRobotNlpResultListener
     */
    public void setiRobotNlpResultListener(IRobotNlpResultListener iRobotNlpResultListener) {
        this.iRobotNlpResultListener = iRobotNlpResultListener;
    }

    /**
     * 注销监听器
     */
    public void removeiRobotNlpResultListener(){
        this.iRobotNlpResultListener = null;
    }


    /**
     * 开始进行语音分析
     * @param text 需要分析的内容
     */
    public void startTextNlp(String text){
        if(nlpManager != null){
            nlpManager.startNLP(text,new IRobotNlpResult());
        }
    }

    /**
     * 用于取消一次语义理解请求
     */
    public void cancalNlp(){
        if(nlpManager != null){
            nlpManager.cancelNLP();
        }
    }

    public class IRobotNlpResult implements NLPCallback{

        @Override
        public void onNLPResult(String s) {
            if(iRobotNlpResultListener != null){
                iRobotNlpResultListener.robotNlpResult(s);
            }
        }

        @Override
        public void onNLPError(String s) {
            if(iRobotNlpResultListener != null){
                iRobotNlpResultListener.robotNlpError(s);
            }
        }
    }
}
